package Hash;

import java.util.HashSet;
import java.util.Set;

public class HashBuck {
    public static class Node {
        int key;
        int val;
        Node next;


    }
    Node[] array = new Node[10];
    private void reSize() {
        Node[] newArray = new Node[array.length*2];
        //处理重新哈希
        for (int i = 0; i < array.length; i++) {
            Node cur = array[i];
            while (cur != null) {
                int index = cur.key % newArray.length;
        //记录下来 之前的cur.next
                Node curNext = cur.next;
        //进行头插法,插入到新数组
                cur.next = newArray[index];
                newArray[index] = cur;
                cur = curNext;
            }
        }
        //把数据给到原数组 array
        array = newArray;
    }

    //石头和宝石
    public int numJewelsInStones(String jewels, String stones) {
        int sum = 0;
        Set<Character> set = new HashSet<>();
        for (int i = 0; i < jewels.length(); i++) {
            set.add(jewels.charAt(i));
        }
        for (int i = 0; i < stones.length(); i++) {
            if(set.contains(stones.charAt(i))) {
                sum++;
            }
        }
        return sum;
    }

    //只出现一次的数字
    public int singleNumber1(int[] nums) {
        int x = 0;
        for (int i = 0; i < nums.length; i++) {
            x ^= nums[i];
        }
        return x;
    }
    public int singleNumber2(int[] nums) {
        Set<Integer> set = new HashSet<>();
        set.add(nums[0]);
        for (int i = 1; i < nums.length; i++) {
            if(set.contains(nums[i])) {
                set.remove(nums[i]);
            } else {
                set.add(nums[i]);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if(set.contains(nums[i])) {
                return nums[i];
            }
        }
        return -1;
    }
}
